package com.hzlj.position.locate.differentiation.positionWarningHandle;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Pair;
import cn.hutool.core.util.ObjectUtil;
import com.hzlj.position.locate.common.dto.position.PositionHandleDTO;
import com.hzlj.position.locate.common.dto.position.PositionQuerySimpleResultDTO;
import com.hzlj.position.locate.common.model.Position;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.stereotype.Service;

import javax.annotation.Nonnull;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 山西-晋城
 * 根据配置来做发送+处理异常
 */
@Slf4j
@Service
public class PositionWarningHandleSxJc extends PositionWarningHandle {
    //key=jzId:warningType
    private final static String POSITION_HANDLE_TIME_CACHE = "locate:handle:time:%s:%s";
    @Resource
    private RedisTemplate<String, PositionHandleDTO> redisTemplate;

    @Override
    public void handlePositions(List<PositionQuerySimpleResultDTO> positions, PositionHandleDTO handle) {
        //今天的时间
        Date now = new Date();
        List<PositionQuerySimpleResultDTO> positionsOfToday = positions.stream()
                .filter(e -> DateUtil.isSameDay(now, e.getPositionTime()) && DateUtil.isSameDay(now, e.getHandleTime()))
                .collect(Collectors.toList());
        if (ObjectUtil.isEmpty(positionsOfToday)) {
            return;
        }

        //存储的处理结果
        PositionHandleDTO handleCache = BeanUtil.copyProperties(handle, PositionHandleDTO.class);
        handleCache.setKeys(null);

        //批量保存处理时间
        redisTemplate.executePipelined(new SessionCallback<Object>() {
            @Override
            public Object execute(@Nonnull RedisOperations operations) throws DataAccessException {
                for (PositionQuerySimpleResultDTO position : positionsOfToday) {
                    String key = String.format(POSITION_HANDLE_TIME_CACHE, position.getJzId(), position.getWarningType());
                    operations.opsForValue().set(key, handleCache, 1, TimeUnit.DAYS);
                }
                return null;
            }
        });
    }


    public List<Pair<Position, PositionHandleDTO>> withHandle(List<Position> positions) {
        if (ObjectUtil.isEmpty(positions)) {
            return Lists.newArrayList();
        }
        return positions.stream().map(e -> {
                    PositionHandleDTO handle = redisTemplate.opsForValue().get(String.format(POSITION_HANDLE_TIME_CACHE, e.getJzId(), e.getWarningType()));
                    return new Pair<>(e, handle);
                })
                .collect(Collectors.toList());
    }


}
